package com.app.bimo.commonbusiness.util;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CenterInside;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * 描述：
 *
 * @author limo on 16/4/25 13:53
 */
public class GlideImagSetUtil {

    /**
     * CenterCrop模式加载本地图片
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadImageCenterCrop(String filename, ImageView imageView, int w, int h) {

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(new ColorDrawable(Color.BLACK))
                .error(new ColorDrawable(Color.BLUE))
                .fallback(new ColorDrawable(Color.RED))
                .override(w, h)
                .centerCrop();


        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


    /**
     * CenterCrop模式加载本地图片
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadImageCenterCrop(String filename, ImageView imageView) {

        RequestOptions requestOptions = new RequestOptions()
                .centerCrop();

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


    /**
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadImage(RequestOptions requestOptions, String filename, ImageView imageView) {
        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadImage(String filename, ImageView imageView) {

        Glide.with(imageView.getContext())
                .load(filename)
                .into(imageView);

    }

    /**
     * 加载banner图
     */
    public static void loadImgRoundedCornersCenterInside(String filename, ImageView imageView, int roundingRadius) {
        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        RequestOptions requestOptions = new RequestOptions()
                .transform(new CenterInside(), roundedCorners)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .skipMemoryCache(false);
        //设置图片圆角角度
        loadImage(requestOptions, filename, imageView);

    }

    /**
     * 加载圆角图片  缓存到内存  缓存到本地磁盘转换后的图片
     *
     * @param filename       地址
     * @param imageView      加载的ImageView
     * @param roundingRadius 圆角度数 px
     */
    public static void loadImgRoundedCornersCenterCrop(String filename, ImageView imageView, int roundingRadius) {
//        int mw = SystemUtil.dip2px(imageView.getContext(), w);

        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        RequestOptions requestOptions = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .skipMemoryCache(false);
        //设置图片圆角角度

        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗

        Glide.with(imageView.getContext())
                .load(filename)
                .priority(Priority.LOW)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * 加载圆角图片  缓存到内存  缓存到本地磁盘转换后的图片
     *
     * @param filename       地址
     * @param imageView      加载的ImageView
     * @param roundingRadius 圆角度数 px
     */
    public static void loadImgRoundedCornersCenterCrop(String filename, ImageView imageView, int placeholder, int roundingRadius) {
//        int mw = SystemUtil.dip2px(imageView.getContext(), w);

        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        RequestOptions requestOptions = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners)
                .placeholder(placeholder)
                .error(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .skipMemoryCache(false);
        //设置图片圆角角度

        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗

        Glide.with(imageView.getContext())
                .load(filename)
                .priority(Priority.LOW)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * 加载圆角图片
     * 按照图片尺寸
     *
     * @param filename       地址
     * @param imageView      加载的ImageView
     * @param roundingRadius 圆角度数 px
     */
    public static void loadImgRoundedCornersCenterCropw(String filename, ImageView imageView, int roundingRadius, int w) {
        int mw = w;

        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);

        RequestOptions requestOptions = new RequestOptions()
                .override(mw, (mw * 109) / 84)
                .transform(new CenterCrop(), roundedCorners)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .skipMemoryCache(true);
        //设置图片圆角角度

        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


    /**
     * CenterCrop模式加载本地图片
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadImageCenterInside(String filename, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .centerInside();
        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


    /**
     * CenterCrop模式加载本地图片
     *
     * @param id        地址
     * @param imageView 加载的ImageView
     */
    public static void loadImageCenterInside(int id, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .centerInside();
        Glide.with(imageView.getContext())
                .load(id)
                .apply(requestOptions)
                .into(imageView);


    }


    /**
     * CenterCrop模式加载圆形图片（如用户头像）
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     * @param replaseid 加载错误  或者其他的id
     */
    public static void loadCicleCenterCrop(String filename, ImageView imageView, int replaseid) {

        RequestOptions requestOptions;

        if (replaseid == 0) {
            requestOptions = new RequestOptions()
                    .circleCrop();
        } else {
            requestOptions = new RequestOptions()
                    .placeholder(replaseid)
                    .error(replaseid)
                    .fallback(replaseid)
                    .circleCrop();
        }

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * CenterCrop模式加载圆形图片（如用户头像）
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadCicleCenterCrop(String filename, ImageView imageView) {
        loadCicleCenterCrop(filename, imageView, 0);
    }


    /**
     * CenterCrop模式加载圆形图片（如用户头像）
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadBookSelfAdImg(String filename, ImageView imageView) {

        RequestOptions requestOptions = new RequestOptions()
                .fitCenter();

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * CenterCrop模式加载圆形图片（如用户头像）
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadBookCenterCrop(String filename, ImageView imageView) {

        RequestOptions requestOptions = new RequestOptions()
                .centerCrop();

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


    /**
     * 高斯模糊加载
     *
     * @param filename  地址
     * @param imageView 加载的ImageView
     */
    public static void loadBlurTrans(String filename, ImageView imageView) {

        RequestOptions requestOptions = new RequestOptions()
                .transform(new BlurTransformation(14));

        Glide.with(imageView.getContext())
                .load(filename)
                .apply(requestOptions)
                .into(imageView);

    }


}
